package com.atguigu.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.TimeUnit;

/**
 * @author DongZH
 * @content
 * @since 2024/3/30 - 16:05
 **/
//@EnableWebMvc   // 禁用boot的默认配置
@Configuration  // 这是一个配置类，给容器中放一个WebMvcConfigurer组件，就能自定义底层
public class MyConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 保留以前的规则
        WebMvcConfigurer.super.addResourceHandlers(registry);

        // 自己写新的规则
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/a/", "classpath:/b/")
                .setCacheControl(CacheControl.maxAge(1180, TimeUnit.SECONDS));

    }

    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override   // 配置静态资源
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                WebMvcConfigurer.super.addResourceHandlers(registry);
                // 自己写新的规则
            }

            @Override   // 配置拦截器
            public void addInterceptors(InterceptorRegistry registry) {

            }
        };
    }
}
